package cn.laixueit.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.laixueit.base.R;
import cn.laixueit.pojo.Category;
import cn.laixueit.service.CategoryService;
import cn.laixueit.utils.CalculateUtils;
import cn.laixueit.vo.BookVo;
import cn.laixueit.vo.SourceVo;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.laixueit.pojo.Source;
import cn.laixueit.service.SourceService;
import cn.laixueit.mapper.SourceMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source>
    implements SourceService{

    @Resource
    private CalculateUtils<Source,SourceVo> calculateUtils;
    @Resource
    private CategoryService categoryService;

    @Override
    public void saveSource(Source source) {
        saveOrUpdate(source);
    }

    @Override
    public R sourcePage(Page<Source> sourcePage, Source source) {
        List<String> sourceTypes = null;
        if(StringUtils.isNotBlank(source.getSourceType())){
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.lambda().apply(String.format("find_in_set(%s,pids)",source.getSourceType()));
            List<Category> categoryList = categoryService.list(wrapper);
            sourceTypes = categoryList.stream().map(x->x.getId().toString()).collect(Collectors.toList());
            sourceTypes.add(source.getSourceType());
        }
        Page<Source> page = page(sourcePage, lambdaQuery()
                .like(StringUtils.isNotBlank(source.getCode()), Source::getCode, source.getCode())
                .in(CollectionUtil.isNotEmpty(sourceTypes),Source::getSourceType,sourceTypes)
                .getWrapper());
        List<Source> records = page.getRecords();
        List<SourceVo> sourceVos = records.stream().map(x -> {
            SourceVo vo = new SourceVo();
            vo = calculateUtils.calculateNewPrice(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return R.ok().put("rows",sourceVos).put("total",page.getTotal());
    }

    @Override
    public void removeSource(String id) {
        removeById(id);
    }

    @Override
    public Source findSource(String id) {
        Source source = getById(id);
        String sourceType = source.getSourceType();
        Category category = categoryService.getOne(categoryService.lambdaQuery()
                .eq(Category::getId,sourceType)
                .getWrapper());
        source.setSourceTypeName(category.getName());
        return source;
    }
}




